package com.macllvm.whale.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.graphics.drawable.Drawable;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.util.Log;

import java.lang.Override;
import java.util.List;
import com.macllvm.whale.entity.FileItem;
import com.macllvm.whale.R;

public class FileAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private List<FileItem> resourceList = null;

    private Drawable dirIcon;
    private Drawable fileIcon;

    private SelectHandler selectHandler;

    static class FileItemView {
        TextView name;
        TextView size;
        ImageView icon;
        CheckBox checkBox;
    }


    public FileAdapter(Context context) {
        Resources res = context.getResources();
        inflater = LayoutInflater.from(context);

        dirIcon = res.getDrawable(R.drawable.directory);
        fileIcon  = res.getDrawable(R.drawable.file);
    }

    public void setDataSource(List<FileItem> list){
        resourceList = list;
    }

    @Override
    public int getCount() {
        return resourceList.size();
    }

    @Override
    public Object getItem(int pos) {
        return resourceList.get(pos);
    }

    @Override
    public long getItemId(int pos) {
        return pos;
    }


    @Override
    public View getView(int pos, View convertView, ViewGroup parent) {
        FileItemView itemView;
        FileItem item = (FileItem)resourceList.get(pos);

        if(convertView == null){
            convertView = inflater.inflate(R.layout.file_item, null);
            itemView = new FileItemView();
            itemView.name = (TextView) convertView.findViewById(R.id.file_name);
            itemView.size = (TextView) convertView.findViewById(R.id.file_size);
            itemView.icon = (ImageView)convertView.findViewById(R.id.file_icon);
            itemView.checkBox = (CheckBox)convertView.findViewById(R.id.file_select);
            convertView.setTag(itemView);
        }
        else{
            itemView = (FileItemView) convertView.getTag();
        }

        itemView.checkBox.setOnCheckedChangeListener(new ItemCheckedChangeListener(pos));
        itemView.name.setText(item.name);
        itemView.checkBox.setChecked(false);

        if(item.type == FileItem.TYPE_DIR) {
            itemView.checkBox.setVisibility(View.GONE);
            itemView.size.setVisibility(View.GONE);
            itemView.icon.setImageDrawable(dirIcon);
        }else {
            itemView.checkBox.setVisibility(View.VISIBLE);
            itemView.size.setText(Long.toString(item.size));
            itemView.icon.setImageDrawable(fileIcon);
        }


        return convertView;
    }

    private class ItemCheckedChangeListener implements OnCheckedChangeListener {
        int index;
        ItemCheckedChangeListener(int i){
            index = i;
        }
        @Override
        public  void 	onCheckedChanged(CompoundButton buttonView, boolean isChecked){
            if(null != selectHandler){
                selectHandler.onSelect(index, isChecked);
            }
        }
    }

    public interface  SelectHandler {
        void onSelect(int index, boolean isChecked);
    }

    public void setSelectHandler(SelectHandler h){
        selectHandler = h;
    }
}
